import { unref } from 'vue'
import { REGEXP_CODE_SIX, REGEXP_EMAIL, REGEXP_PHONE, REGEXP_PWD } from '../../config'

/**
 * 是否是数字
 * @param value
 */
function isNumber(value) {
    if (typeof value === 'string' && value.trim() === '') {
        return false
    }
    return window.isNaN(Number(value)) == false
}

/**
 * 是否是整数
 * @param value
 */
function isInt(value) {
    if (typeof value === 'string' && value.trim() === '') {
        return false
    }
    if (Number.isInteger(Number(value)) == false) {
        return false
    }
    return true
}

export class Rules {
    rules = []
    constructor() {}

    get value() {
        return this.rules
    }

    /**
     * 插入一个新的规则
     * @param rule
     */
    push(rule) {
        this.rules.push(rule)
        return this
    }

    /**
     * 删除指定的第几条规则
     * @param index
     */
    delete(index) {
        this.rules.splice(index, 1)
    }

    /**
     * 必填
     * @param message
     * @param trigger
     */
    required(message = '不能为空', trigger = ['input', 'blur', 'change']) {
        this.rules.push({
            required: true,
            validator(rule, value) {
                if (value === null || value === undefined || value === '') {
                    return new Error(message)
                }
                if (value instanceof Array && !value.length) {
                    return new Error(message)
                }
                return true
            },
            message,
            trigger
        })
        return this
    }

    /**
     * 最小长度
     * @param length
     * @param message
     * @param trigger
     */
    minLength(length, message = `长度不能小于${length}位`, trigger = ['input', 'blur']) {
        this.rules.push({
            validator(rule, value) {
                if (value === '') {
                    return true
                }
                if (value.length < length) {
                    return new Error(message)
                }
                return true
            },
            trigger
        })
        return this
    }

    /**
     * 最大长度
     * @param length
     * @param message
     * @param trigger
     */
    maxLength(length, message = `长度不能超过${length}位`, trigger = ['input', 'blur']) {
        this.rules.push({
            validator(rule, value) {
                if (value === '') {
                    return true
                }
                if (value && value.length > length) {
                    return new Error(message)
                }
                return true
            },
            trigger
        })
        return this
    }
    /**
     * 指定长度
     * @param length
     * @param message
     * @param trigger
     */
    length(length, message = `字符长度需要等于${length}位`, trigger = ['input', 'blur']) {
        this.rules.push({
            validator(rule, value) {
                if (value === '') {
                    return true
                }
                if (value && value.length != length) {
                    return new Error(message)
                }
                return true
            },
            trigger
        })
        return this
    }

    /**
     * 与指定值相等
     * @param compareValue
     * @param message
     * @param trigger
     */
    eq(compareValue, message = '', trigger = ['input', 'blur']) {
        this.rules.push({
            validator(rule, value) {
                if (value === '') {
                    return true
                }
                if (value !== unref(compareValue)) {
                    return new Error(message ? message : `必须等于${unref(compareValue)}`)
                }
                return true
            },
            trigger
        })
        return this
    }

    /**
     * 大于指定值
     * @param compareValue
     * @param message
     * @param trigger
     */
    gt(compareValue, message = '', trigger = ['input', 'blur']) {
        this.rules.push({
            validator(rule, value) {
                if (value === '') {
                    return true
                }
                const v = unref(compareValue)
                if (isNumber(value) == false || isNumber(v) == false) {
                    return new Error('请输入数字')
                }
                if (value <= v) {
                    return new Error(message ? message : `必须大于${v}`)
                }
                return true
            },
            trigger
        })
        return this
    }
    /**
     * 大于等于指定值
     * @param compareValue
     * @param message
     * @param trigger
     */
    gte(compareValue, message = '', trigger = ['input', 'blur']) {
        this.rules.push({
            validator(rule, value) {
                if (value === '') {
                    return true
                }
                const v = unref(compareValue)
                if (isNumber(value) == false || isNumber(v) == false) {
                    return new Error('请输入数字')
                }
                if (value < v) {
                    return new Error(message ? message : `必须大于等于${v}`)
                }
                return true
            },
            trigger
        })
        return this
    }
    /**
     * 小于指定值
     * @param compareValue
     * @param message
     * @param trigger
     */
    lt(compareValue, message, trigger = ['input', 'blur']) {
        this.rules.push({
            validator(rule, value) {
                if (value === '') {
                    return true
                }
                const v = unref(compareValue)
                if (isNumber(value) == false || isNumber(v) == false) {
                    return new Error('请输入数字')
                }
                if (value >= v) {
                    return new Error(message ? message : `必须小于${v}`)
                }
                return true
            },
            trigger
        })
        return this
    }
    /**
     * 小于等于指定值
     * @param compareValue
     * @param message
     * @param trigger
     */
    lte(compareValue, message = '', trigger = ['input', 'blur']) {
        this.rules.push({
            validator(rule, value) {
                if (value === '') {
                    return true
                }
                const v = unref(compareValue)
                if (isNumber(value) == false || isNumber(v) == false) {
                    return new Error('请输入数字')
                }
                if (value > v) {
                    return new Error(message ? message : `必须小于等于${v}`)
                }
                return true
            },
            trigger
        })
        return this
    }
    /**
     * 是整数
     * @param message
     * @param trigger
     */
    _int(message = `必须是整数`, trigger = ['input', 'blur']) {
        this.rules.push({
            validator(rule, value) {
                if (value === '') {
                    return true
                }
                if (isInt(value) == false) {
                    return new Error(message)
                }
                return true
            },
            trigger
        })
        return this
    }

    /**
     * 正整数
     * @param message
     * @param trigger
     */
    _pint(message = `必须是正整数`, trigger = ['input', 'blur']) {
        this.rules.push({
            validator(rule, value) {
                if (value === '') {
                    return true
                }
                if (isInt(value) == false) {
                    return new Error(message)
                }
                if (Number(value) <= 0) {
                    return new Error(message)
                }
                return true
            },
            trigger
        })
        return this
    }

    /**
     * 自然数：0 和 正整数
     * @param message
     * @param trigger
     */
    _natural(message = `必须是大于等于0的整数`, trigger = ['input', 'blur']) {
        this.rules.push({
            validator(rule, value) {
                if (value === '') {
                    return true
                }
                if (isInt(value) == false) {
                    return new Error(message)
                }
                if (Number(value) < 0) {
                    return new Error(message)
                }
                return true
            },
            trigger
        })
        return this
    }

    /**
     * 数字
     * @param message
     * @param trigger
     */
    _number(message = `必须是数字`, trigger = ['input', 'blur']) {
        this.rules.push({
            validator(rule, value) {
                if (value === '') {
                    return true
                }
                if (isNumber(value) == false) {
                    return new Error(message)
                }
                return true
            },
            trigger
        })
        return this
    }

    /**
     * 正数
     * @param message
     * @param trigger
     */
    _pnumber(message = `必须是正数`, trigger = ['input', 'blur']) {
        this.rules.push({
            validator(rule, value) {
                if (value === '') {
                    return true
                }
                if (isNumber(value) == false) {
                    return new Error(message)
                }
                if (Number(value) <= 0) {
                    return new Error(message)
                }
                return true
            },
            trigger
        })
        return this
    }

    /**
     * 正数和0
     * @param message
     * @param trigger
     */
    _zpnumber(message = `必须是正数`, trigger = ['input', 'blur']) {
        this.rules.push({
            validator(rule, value) {
                if (value === '') {
                    return true
                }
                if (isNumber(value) == false) {
                    return new Error(message)
                }
                if (Number(value) <= 0) {
                    return new Error(message)
                }
                return true
            },
            trigger
        })
        return this
    }
    _boolean(message = `请选择`, trigger = ['input', 'blur']) {
        this.rules.push({
            validator(rule, value) {
                if (value === '') {
                    return true
                }
                value += ''
                if (value != '1' && value != '0' && value != 'true' && value != 'false') {
                    return new Error(message)
                }
                return true
            },
            trigger
        })
        return this
    }
    /**
     * 手机号
     * @param message
     * @param trigger
     */
    phone(message = `手机号格式不正确`, trigger = ['input', 'blur']) {
        this.rules.push({ pattern: REGEXP_PHONE, message, trigger })
        return this
    }

    email(message = `邮箱格式不正确`, trigger = ['input', 'blur']) {
        this.rules.push({ pattern: REGEXP_EMAIL, message, trigger })
        return this
    }
}

/** 创建自定义错误信息的必填表单规则 */
export const createRequiredFormRule = (message = '不能为空') => ({
    required: true,
    message
})

export const requiredFormRule = createRequiredFormRule()

/** 表单规则 */
export const formRules = {
    phone: [
        createRequiredFormRule('请输入手机号码'),
        { pattern: REGEXP_PHONE, message: '手机号码格式错误', trigger: 'input' }
    ],
    pwd: [
        createRequiredFormRule('请输入密码'),
        {
            pattern: REGEXP_PWD,
            message: '密码为6-18位数字/字符/符号，至少2种组合',
            trigger: 'input'
        }
    ],
    code: [
        createRequiredFormRule('请输入验证码'),
        {
            pattern: REGEXP_CODE_SIX,
            message: '验证码格式错误',
            trigger: 'input'
        }
    ],
    email: [{ pattern: REGEXP_EMAIL, message: '邮箱格式错误', trigger: 'blur' }]
}

/** 是否为空字符串 */
function isBlankString(str) {
    return str.trim() === ''
}

/** 获取确认密码的表单规则 */
export function getConfirmPwdRule(pwd) {
    const confirmPwdRule = [
        { required: true, message: '请输入确认密码' },
        {
            validator: (rule, value) => {
                console.log('pwd', pwd)
                if (!isBlankString(value) && value !== pwd.newPassword1) {
                    return Promise.reject(rule.message)
                }
                return Promise.resolve()
            },
            message: '输入的值与密码不一致',
            trigger: 'input'
        }
    ]
    return confirmPwdRule
}

/** 获取图片验证码的表单规则 */
export function getImgCodeRule(imgCode) {
    const imgCodeRule = [
        { required: true, message: '请输入验证码' },
        {
            validator: (rule, value) => {
                if (!isBlankString(value) && value !== imgCode.value) {
                    return Promise.reject(rule.message)
                }
                return Promise.resolve()
            },
            message: '验证码不正确',
            trigger: 'blur'
        }
    ]
    return imgCodeRule
}
